/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.pages.member.entrust')
        .controller('ParentEntrustCtrl', ParentEntrustCtrl);

    function ParentEntrustCtrl($scope,$state,AppUtils,LoginAccountAuth,EntrustService,$sce,$interval,$timeout,$ionicScrollDelegate) {

        var vm = this;
        vm.loginAccount = {};
        vm.exhortaions = [];
        vm.loadMoreBtnStatus = true;
        vm.loadMoreBtnStr = "加载更多";
        vm.pageIndex = 0;//第一页

        //首次进入
        LoginAccountAuth.getAccount().then(function(data){
            vm.loginAccount = data;
        });

        //往上拉加载更多
        $scope.loadMoreEntrust = function(){
            if(vm.loadMoreBtnStatus){
                vm.pageIndex = vm.pageIndex + 1;
                //vm.pageIndex = 1;
                vm.getEntrustPage();
            }
        }

        vm.getEntrustPage = function(){
            var params = {};
            params.pageInfo = {};
            params.pageInfo.pageSize = 10;
            params.pageInfo.pageNumber = vm.pageIndex;

            EntrustService.loadEntrustPage(params).then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    var reponseDate = response.pageResult;
                    var items = reponseDate.pageResult;
                    var pageIndex = reponseDate.pageIndex;
                    var pageTotalPageSize = reponseDate.pageTotalPageSize;
                    if(pageIndex == pageTotalPageSize || items.length == 0){
                        vm.loadMoreBtnStatus = false;
                        vm.loadMoreBtnStr = "没有更多数据了";
                    }
                    angular.forEach(items,function(data){
                        vm.exhortaions.push(data);
                    })
                }
            }).finally(function() {
                // Stop the ion-refresher from spinning
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
        }


        $scope.gotoDetail = function(entrustId){
            if(!AppUtils.isUndefinedOrNull(entrustId)){
                $state.go("entrust.entrustInfo",{entrustId:entrustId});
            }else{
                AppUtils.showAlert("请选择要查看的嘱托！");
            }
        }

        //音频播放
        vm.audioPlaying = false;
        $scope.playAudio = function(audioPath,divId){
            var audio = $("audio[name='"+divId+"']")[0];
            var interval;
            if(AppUtils.isUndefinedOrNull(audio)){
                var entrustBox = $("#" + divId);
                entrustBox.addClass("entrust-audio-loading");
                var audioHtml = '<audio name="'+ divId +'" src="'+ $sce.trustAsResourceUrl(audioPath) +'" controls style="display: none;"></audio>';
                $("#audioBox").append(audioHtml);
                audio = $("audio[name='"+divId+"']")[0];
                //音频可以加载至播放
                audio.addEventListener("playing", function() {
                    var nprogress = entrustBox.find(".audio-nprogress")[0];
                    var duration = Math.ceil(audio.duration*500);//总毫秒数
                    var count = duration/50;
                    var i = 100/count * 1000;
                    var s = 0;
                    interval = $interval(function(){
                        s = s + i;
                        nprogress.style.width =  s/1000 + "%";
                    }, 50, count);
                    entrustBox.find("button").removeClass("ion-play").addClass("ion-pause");
                }, false);
                //音频可以播放完毕
                audio.addEventListener("ended", function() {
                    $timeout(function(){
                        entrustBox.find("button").removeClass("ion-pause").addClass("ion-play");
                        entrustBox.find(".audio-nprogress")[0].style.width = "0%";
                        $interval.cancel(interval);
                        vm.audioPlaying = false;
                    },200)
                }, false);
                audio.addEventListener("canplay", function() {
                    entrustBox.removeClass("entrust-audio-loading");
                    vm.audioPlaying = true;
                    audio.play();
                }, false);
            }else{
                audio.play();
            }
        }

        //悬浮菜单
        $("#hamburgerMenuBtn").on("click",function () {
            $("#hamburgerMenuBtn").toggleClass("active");
        });

        var lastTop = 0;
        $scope.scrollFun = function(){
            var top = $ionicScrollDelegate.$getByHandle('entrustScroll').getScrollPosition().top;
            if(top > 200 && top > lastTop){
                $("#hamburgerMenuBtn").removeClass("active");
                $("#hamburgerMenuBtn").css("right","-80px")
            }else if(lastTop - top > 20){
                $("#hamburgerMenuBtn").css("right","40px")
            }
            lastTop = top;
        }
    }

})();